package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.example.codeutils.utils.TimeUtils;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import master_bean.DealerBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

import static com.bjesc_b2c.app.appraiset.R.id.iv_return;

/**
 * Created by yidong on 2017/8/11.
 */

public class DealerDetailsActivity extends BaseActivity {
    @Bind(R.id.tv_dealer_name)
    TextView dealerName;
    @Bind(R.id.tv_dealer_address)
    TextView dealerAddress;
    @Bind(R.id.tv_dealer_id)
    TextView dealerId;
    @Bind(R.id.tv_dealer_phone)
    TextView dealerPhone;
    @Bind(R.id.tv_dealer_time)
    TextView dealerTime;
    @Bind(R.id.tv_dealer_await_sales)
    TextView dealerAwaitSeles;
    @Bind(R.id.tv_dealer_total_amount)
    TextView dealerToalAmunt;
    @Bind(R.id.tv_dealer_car_amount)
    TextView dealerCarAmount;
    @Bind(R.id.tv_dealer_sales_amount)
    TextView dealerSalesAmount;
    @Bind(R.id.tv_maintenance_person)
    TextView maintenancePerson;
    @Bind(iv_return)
    LinearLayout returnLin;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.linear_bottom)
    LinearLayout linearLayout;
    @Bind(R.id.btn_distribution)
    Button distribution;
    private Intent mIntent;
    private String dealerID ;
    private int clueType;
    @Override
    protected int getLayout() {
        return R.layout.activity_dealer_details;
    }

    @Override
    protected void initEventAndData() {
        titleTv.setText("经销商详情");
        returnLin.setVisibility(View.VISIBLE);
        mIntent =  new Intent();
        clueType = getIntent().getIntExtra("clueType",1);
        if (clueType == 3) {
            linearLayout.setVisibility(View.GONE);
            distribution.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void createPresenter() {
        dealerID = getIntent().getStringExtra("carFofId");
        getDealerInfo(dealerID);
    }

    @OnClick({R.id.btn_car_list, R.id.btn_supplement,R.id.iv_return,R.id.btn_distribution})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn_car_list:
                mIntent.setClass(DealerDetailsActivity.this,DealerUploadCar.class);
                mIntent.putExtra("carFofId",dealerID);
                mIntent.putExtra("Title","上传车源列表");
                startIntent(mIntent,false);
                break;
            case R.id.btn_supplement:
                mIntent.setClass(DealerDetailsActivity.this,AddDealerActivity.class);
                mIntent.putExtra("carFofId",dealerID);
                mIntent.putExtra("Title","编辑经销商信息");
                mIntent.putExtra("Edit",true);
                startIntent(mIntent,false);
                break;
            case R.id.iv_return:
                finish();
                break;
            case R.id.btn_distribution:
                mIntent.setClass(DealerDetailsActivity.this,DistributionActivity.class);
                mIntent.putExtra("carFofId",dealerID);
                startIntent(mIntent,false);
                break;
        }
    }

    private void setDate(DealerBean bean) {
        dealerName.setText(bean.getDealer_name());
        dealerAddress.setText(bean.getContact_address());
        dealerId.setText(bean.getId());
        dealerPhone.setText(bean.getPhone());
        dealerTime.setText(TimeUtils.millis2String(bean.getAdd_time()));
        dealerAwaitSeles.setText(bean.getPUBLISHPASS());
        dealerToalAmunt.setText(bean.getSOLD());
        dealerCarAmount.setText(bean.getEntry_count());
        dealerSalesAmount.setText(bean.getMonth_sale());
        maintenancePerson.setText(bean.getAgent_nickname());
    }

    private void getDealerInfo(String dealer_id) {
        HashMap<String, Object> build = new MapUtil().put("dealer_id", dealer_id).build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(DealerDetailsActivity.this, build).create(IApiActivityRequest.class).getDealerInfo()
                , new BaseSubscriber<TokenBean<DealerBean>>(DealerDetailsActivity.this) {
                    @Override
                    public void onSuccess(TokenBean<DealerBean> o) {
                        if (o.getData() != null)
                            setDate(o.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {

                        cancelLoading();
                    }

                    @Override
                    public void onNext(TokenBean<DealerBean> o) {
                        super.onNext(o);
                    }

                    @Override
                    public void onStart() {
                        showLoading();
                    }

                    @Override
                    public void onCompleted() {
                        cancelLoading();
                    }
                });
    }
}
